package com.house.consume.send;

import com.house.common.utils.R;
import com.house.consume.feign.FeignThreepartService;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName SendCode
 * @Description TODO
 * @Author
 * @DATE 2022/4/22 15:26
 */
@Component
@RabbitListener(queues = "sendCode")
public class SendCode {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private FeignThreepartService feignThreepartService;

    @RabbitHandler
    public void sendCode(String phone) {
        //random.nextInt(8999) 返回值为 0-8999
        //random.nextInt(8999)+1000 返回值则为 1000-9999
        Integer code = new Random().nextInt(8999) + 1000;

        Map<String, Object> map = new HashMap<>();
        map.put("code", code.toString());
        map.put("phone", phone);
        feignThreepartService.sendCOde(map);
        redisTemplate.opsForValue().set("consume:sendCode:" + phone, code.toString(), 3000, TimeUnit.SECONDS);

    }

}
